package org.dromara.blog.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.blog.domain.vo.BlogArticleVo;
import org.dromara.blog.domain.bo.BlogArticleBo;
import org.dromara.blog.service.IBlogArticleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 博客文章
 *
 * @author LSY
 * @date 2025-11-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/article")
public class BlogArticleController extends BaseController {

    private final IBlogArticleService blogArticleService;

    /**
     * 查询博客文章列表
     */
    @SaCheckPermission("blog:article:list")
    @GetMapping("/list")
    public TableDataInfo<BlogArticleVo> list(BlogArticleBo bo, PageQuery pageQuery) {
        return blogArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出博客文章列表
     */
    @SaCheckPermission("blog:article:export")
    @Log(title = "博客文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogArticleBo bo, HttpServletResponse response) {
        List<BlogArticleVo> list = blogArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "博客文章", BlogArticleVo.class, response);
    }

    /**
     * 获取博客文章详细信息
     *
     * @param articleId 主键
     */
    @SaCheckPermission("blog:article:query")
    @GetMapping("/{articleId}")
    public R<BlogArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long articleId) {
        return R.ok(blogArticleService.queryById(articleId));
    }

    /**
     * 新增博客文章
     */
    @SaCheckPermission("blog:article:add")
    @Log(title = "博客文章", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(blogArticleService.insertByBo(bo));
    }

    /**
     * 修改博客文章
     */
    @SaCheckPermission("blog:article:edit")
    @Log(title = "博客文章", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(blogArticleService.updateByBo(bo));
    }

    /**
     * 删除博客文章
     *
     * @param articleIds 主键串
     */
    @SaCheckPermission("blog:article:remove")
    @Log(title = "博客文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] articleIds) {
        return toAjax(blogArticleService.deleteWithValidByIds(List.of(articleIds), true));
    }
}
